package cn.dliyy.warehouse.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.persistence.Column;
import javax.validation.constraints.NotNull;

import org.hibernate.annotations.Type;
import org.hibernate.validator.constraints.NotEmpty;
import org.joda.time.DateTime;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.format.annotation.NumberFormat;
import org.springframework.format.annotation.DateTimeFormat.ISO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.dliyy.base.bean.Constant;
import cn.dliyy.base.bean.KeyValue;
import cn.dliyy.base.service.CommonService;
import cn.dliyy.base.service.KeyValueService;
import cn.dliyy.staff.bean.UStaff;
import cn.dliyy.warehouse.bean.BBrand;
import cn.dliyy.warehouse.bean.BDealer;
import cn.dliyy.warehouse.bean.BGoods;
import cn.dliyy.warehouse.bean.BUnit;
import cn.dliyy.warehouse.bean.BCategory;
import cn.dliyy.warehouse.bean.WItems;
import cn.dliyy.warehouse.bean.WItems;
import cn.dliyy.warehouse.bean.SStorage;
import cn.dliyy.warehouse.bean.BWarehouse;

@Service
public class SStorageService extends CommonService {
  @Resource
  private KeyValueService kvService;

  @Override
  public List<?> getList(Class<?> clazz) {
    return (List<?>) getList();
  }

  public List<?> getList() {
    String hql = "from SItems a order by a.makeDate";
    List<?> itemlist = entityDao.createQuery(hql);
    // List<?> itemlist = super.getList(SItems.class);
    List<SStorage> storagelist = new ArrayList<SStorage>();
    if (itemlist == null)
      return null;
    for (int i = 0; i < itemlist.size(); i++) {
      WItems titem = (WItems) itemlist.get(i);
      storagelist.add(this.get(titem));
    }

    return storagelist;
  }

  public SStorage getLatest() {
    String hql = "from SItems a order by a.makeDate DESC";
    List<Object> itemlist = entityDao.createQuery(hql, 0, 1);
    SStorage s = new SStorage();
    if (itemlist == null || itemlist.size() < 1)
      return s;
    WItems items = (WItems) itemlist.get(0);
    s = this.get(items);
    return s;
  }

  /**
   * id 是SItems.id 返回 Storage 对象
   * 
   * @param id
   * @return
   */
  public SStorage get(String id) {
    WItems titem = (WItems) super.get(WItems.class, id);
    return this.get(titem);
  }

  public SStorage get() {
    SStorage ts = new SStorage();
    Map<String, String> reasonMap = kvService.getMap(Constant.STORAGE_IN);
    ts.setReasonMap(reasonMap);
    return ts;
  }

  @Override
  @Transactional
  public void save(Object sStorage) {
    SStorage sst = (SStorage) sStorage;

    if (entityDao.get(UStaff.class, sst.getItems().getHandlerid()) == null) {
      // Error;
    }
    if (entityDao.get(UStaff.class, sst.getItems().getOperatorid()) == null) {

    }
    BCategory tcategory = (BCategory) entityDao.get(BCategory.class, sst.getItems().getCateid());
    BGoods tbgoods = (BGoods) entityDao.get(BGoods.class, sst.getItems().getGoodsid());
    if (tcategory != null && tbgoods != null) {
      if (!tcategory.getId().equals(tbgoods.getCategoryId())) {
        // error
      }
    } else {
      // error
    }

    entityDao.save(sst.getItems());
  }

  /**
   * 传入SItems对象，然后返回SStorage对象，其中SStorage对象中的Items属性值为传入值。
   * 
   * @param titem
   * @return
   */
  private SStorage get(WItems titem) {
    entityDao.get(SItemsa.class,"1");
    SStorage ts = new SStorage();
    BGoods tgoods = (BGoods) super.get(BGoods.class, titem.getGoodsid());
    BCategory tcategory = (BCategory) super.get(BCategory.class, titem.getCateid());
    UStaff thandler = (UStaff) super.get(UStaff.class, titem.getHandlerid());
    UStaff toperator = (UStaff) super.get(UStaff.class, titem.getOperatorid());
    BWarehouse twarehouse = (BWarehouse) super.get(BWarehouse.class, titem.getWarehouseid());
    BDealer tdealer = (BDealer) super.get(BDealer.class, titem.getDealerid());
    BBrand tbrand = (BBrand) super.get(BBrand.class, titem.getBrandid());
    BUnit tunit = (BUnit) super.get(BUnit.class, tgoods.getUnitId());
    KeyValue treason = new KeyValue();
    treason.setCode(titem.getReasonid());
    treason.setCodeType(Constant.STORAGE_IN);
    // treason = (KeyValue) kvService.get(treason);
    Map<String, String> reasonMap = kvService.getMap(Constant.STORAGE_IN);
    treason.setValue(reasonMap.get(titem.getReasonid()));
    ts.setItems(titem);
    ts.setGoods(tgoods);
    ts.setCategory(tcategory);
    ts.setHandler(thandler);
    ts.setOperator(toperator);
    ts.setWarehouse(twarehouse);
    ts.setDealer(tdealer);
    ts.setBrand(tbrand);
    ts.setReason(treason);
    ts.setReasonMap(reasonMap);
    ts.setUnit(tunit);
    return ts;
  }

}
